﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Net.Mail;
using System.Data;
using MYNAMESPACE;

namespace final_project
{
    public partial class ATM_recharge : Form
    {
        private connect_db con;
        public SmtpClient client = new SmtpClient();
        public MailMessage msg = new MailMessage();
        public System.Net.NetworkCredential smpt =
            new System.Net.NetworkCredential("hima74105@gmail.com", "0105674105");

        public string nm2;
        public string passvalue
        {

            get
            {
                return nm2;
            }
            set
            {
                nm2 = value;
            }

        }
        public string nm3;
        public string passvalue1
        {

            get
            {
                return nm3;
            }
            set
            {
                nm3 = value;
            }

        }
        public string nm5;
        public string passvalue6
        {

            get
            {
                return nm5;
            }
            set
            {
                nm5 = value;
            }

        }
        public string nm4;
        public string passvalue5
        {

            get
            {
                return nm4;
            }
            set
            {
                nm4 = value;
            }
        }

        public ATM_recharge()
        {
            InitializeComponent();
        }

        public void SendEmail(string sendTo, string sendFrom, string subject, string body)
        {
            try
            {
                client.Host = "smtp.gmail.com";
                client.Port = 587;
                client.UseDefaultCredentials = false;
                client.Credentials = smpt;
                client.EnableSsl = true;

                MailAddress to = new MailAddress(sendTo);
                MailAddress from = new MailAddress(sendFrom);

                msg.Subject = subject;
                msg.Body = body;
                msg.From = from;
                msg.To.Add(to);

                client.Send(msg);



            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "error");

            }

        }

        private void ATM_recharge_Load(object sender, EventArgs e)
        {
            ID.Text = nm2;
            Balance.Text = nm3;
            Phone.Text = nm4;
            Company_ID.Text = nm5;
        }

        private void button1_Click(object sender, EventArgs e)
        {

            // Validating Start code

            if (this.ValidateChildren(ValidationConstraints.Enabled))
            {
                MessageBox.Show("All controls are valid!");
                //Logic to save...
            }
            else
            {
                MessageBox.Show("There are invalid controls on the form.");
                //Return user to form...
            }

            //Validating end code 

            
            user us = new user();
            if (How_Much.Text.Length == null)
            {
                this.Hide();
                MessageBox.Show("must have value");
                ATM_recharge Ac2 = new ATM_recharge();
                Ac2.passvalue = ID.Text;
                Ac2.ShowDialog();

            }
            else
            {

                us.setnational_id(Int32.Parse(ID.Text));
                us.setvalue(double.Parse(How_Much.Text));
                us.setbalance(double.Parse(Balance.Text));
                if( us.Recharge_withdrwal(double.Parse(How_Much.Text)))
                {
                        // code
                        
                        con = new connect_db();
                        con.sqlquery("update userdata set balance = (@namep1) where id= (@namep) ");
                        con.cmd.Parameters.Add("@namep", us.getnational_id());
                        con.cmd.Parameters.Add("@namep1", us.getbalance());
                        con.sqlnonex();
                        con.sqlquery("select balance from userdata where id=(@namep)");
                        con.cmd.Parameters.Add("@namep", us.getnational_id());
                        foreach (DataRow dr in con.QueryEx().Rows)
                        {

                            MessageBox.Show("the balance is " + dr[0].ToString());

                        }

                        user cou = new user();

                        cou.setco_id(string.Format(Company_ID.Text));
                        con = new connect_db();
                        con.sqlquery("select balance from companydata where name=(@namep)");
                        con.cmd.Parameters.Add("@namep", cou.getco_id());
                        foreach (DataRow dr in con.QueryEx().Rows)
                        {

                            Company_balance.Text = dr[0].ToString();


                        }

                       
                        cou.setbalance(double.Parse(Company_balance.Text));
                        cou.deposite(double.Parse(How_Much.Text));
                        con = new connect_db();
                        con.sqlquery("update companydata set balance=(@namep1)  where name= (@namep2)");
                        con.cmd.Parameters.Add("@namep2", cou.getco_id());
                        con.cmd.Parameters.Add("@namep1", cou.getbalance());
                        con.sqlnonex();
                        con.sqlquery("select companydata.mail from userdata , companydata where  userdata.Co_id=companydata.Co_id and id=(@namep)");
                        con.cmd.Parameters.Add("@namep", ID.Text.Trim());
                        foreach (DataRow dr in con.QueryEx().Rows)
                        {
                            SendEmail(dr[0].ToString() + "@gmail.com", "hima4105@gmail.com", "Bank System", "please send    " + How_Much.Text + "to this  phone  is   " + Phone.Text);
                        }
                        this.Hide();
                        this.Hide();
                        ATM_Account AA = new ATM_Account();
                        AA.passvalue = ID.Text;
                        AA.ShowDialog();
                    }
                    else
                    {
                        MessageBox.Show("maximum value is 500");
                    }

                
            }

        }

        private void How_Much_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(How_Much.Text))
            {

                e.Cancel = true;
                How_Much.Focus();
                errorProvider1.SetError(How_Much, "Salary Must Be Entered !");

            }
            else if (!Regex.IsMatch(How_Much.Text, @"^\d+$"))
            {
                e.Cancel = true;
                errorProvider1.SetError(How_Much, "No Negative Numbers");

            }
            else
            {
                e.Cancel = false;
                errorProvider1.SetError(How_Much, "");
            }
        }
    }
}
